package com.dymall.cart.controller;
import com.dymall.cart.domain.dto.CartDTO;
import com.dymall.cart.domain.dto.CartItemDTO;
import com.dymall.cart.domain.req.AddItemReq;
import com.dymall.cart.service.impl.CartServiceImpl;
import com.dymall.common.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/carts")
public class CartController {
    @Autowired
    private CartServiceImpl cartService;

    public Long getUserId(){
        return UserContext.getUser();
    }

    /**
     * 查看购物车商品
     * @return cart
     */
    @GetMapping
    public CartDTO getCart(){
        Long user = getUserId();
        return cartService.getCart(user);
    }

    /**
     * 添加商品到购物车
     * @param cartItem
     */
    @PostMapping
    public void addItem(@RequestBody CartItemDTO cartItem){
        cartService.addItem(getUserId(),cartItem);
    }

    /**
     * 从购物车移除一个商品
     * @param itemId
     */
    @DeleteMapping("/{id}")
    public void deleteItemById(@PathVariable("id") Long itemId){
        cartService.removeAItem(itemId);
    }

    /**
     * 批量删除购物车商品
     * @param itemIds
     */
    @DeleteMapping("/items")
    public void deleteItemByIds(@RequestParam("ids") List<Long> itemIds){
        cartService.removeItems(itemIds);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/empty")
    public void emptyCart(){
        Long userId = getUserId();
        cartService.emptyCart(userId);
    }
}
